﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ISB_iCard
{
    public partial class PendingTxns : Form
    {
        public PendingTxns()
        {
            InitializeComponent();
        }
        LocalDB objLocalDB = new LocalDB();
        List<AllTxn> lstAll = null;
        iCardService.Service1 objiCardService = new ISB_iCard.iCardService.Service1();
        iCardService.Result objResult = new ISB_iCard.iCardService.Result();

        private void PendingTxns_Load(object sender, EventArgs e)
        {
            RefreshList();
            this.KeyDown += new KeyEventHandler(this.OnKeyDown);
            objiCardService.Url = Convert.ToString(MobileConfiguration.Settings["WSURL"]);
            listView1.Focus();
        }

        private bool CheckWiFi()
        {
            if (Utility.CheckWiFi())
                return true;
            else
                return false;
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Hide();
                    //MessageBox.Show("Pressed Escape Key");
                    break;

                case Keys.F1:
                    if (CheckWiFi())
                        btnRefresh_Click(new Object(), new EventArgs());
                    else
                        MessageBox.Show("NW Not available.");
                    break;

                case Keys.F2:
                    if (CheckWiFi())
                        btnUpdate_Click(sender, e);
                    else
                        MessageBox.Show("NW Not available.");
                    break;
            }
        }
        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Hide();
                    //MessageBox.Show("Pressed Escape Key");
                    break;

                case Keys.F1:
                    if (CheckWiFi())
                        btnRefresh_Click(new Object(), new EventArgs());
                    else
                        MessageBox.Show("NW Not available.");
                    break;

                case Keys.F2:
                    if (CheckWiFi())
                        btnUpdate_Click(sender, e);
                    else
                        MessageBox.Show("NW Not available.");
                    break;
            }
        }

        private void RefreshList()
        {
            btnRefresh.Enabled = false;
            btnUpdate.Enabled = false;
            try
            {
                lstAll = objLocalDB.All();
                lblRecordCount.Text = lstAll.Count.ToString();
                listView1.Items.Clear();
                for (int i = 0; i < lstAll.Count; i++)
                {
                    ListViewItem li = new ListViewItem((i + 1).ToString());
                    li.SubItems.Add(lstAll[i].txnID);
                    li.SubItems.Add(lstAll[i].CreateDate);
                    listView1.Items.Add(li);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            btnRefresh.Enabled = true;
            btnUpdate.Enabled = true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {


                statusBar1.Text = "Updating pending items..";
                statusBar1.Refresh();
                btnRefresh.Enabled = false;
                btnUpdate.Enabled = false;
                if (Utility.CheckWiFi())
                {
                    for (int i = 0; i < lstAll.Count; i++)
                    {
                        if (lstAll[i].Status == false && lstAll[i].IsError == false)
                        {
                            objResult = objiCardService.PerformHHTransaction(Convert.ToInt64(lstAll[i].txnID), lstAll[i].InstituteId, "0");
                            if (objResult.IsSuccess)
                            {
                                objLocalDB.Pop(Convert.ToInt64(lstAll[i].txnID));
                            }
                            else
                                MessageBox.Show("Error for TXN: " + lstAll[i].txnID + ". " + objResult.ErrorMessage + ". Click ok to continue");
                        }
                    }
                    MessageBox.Show("Updating completed.");
                }
                else
                    MessageBox.Show("NW not available.");
                statusBar1.Text = "Updating completed.";
                statusBar1.Refresh();
                btnRefresh.Enabled = true;
                btnUpdate.Enabled = true;
                RefreshList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

    }
}